package incidencies.android.es;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class HttpUtil {

	/** @return a DefaultHttpClient that timeouts after 30s/10s. */
	private DefaultHttpClient getNewHttpClient(){
		//Parametres de Timeout:
		HttpParams httpParameters = new BasicHttpParams();
		// Set the timeout in milliseconds until a connection is established.
		HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
		// Set the default socket timeout (SO_TIMEOUT) in milliseconds which is the timeout for waiting for data.
		HttpConnectionParams.setSoTimeout(httpParameters, 10000);
		// Per les linies d'amunt, check http://stackoverflow.com/questions/693997/how-to-set-httpresponse-timeout-for-android-in-java
		
		// Turn off stale checking. Our connections break all the time anyway, and it's not worth it to pay the penalty of checking every time.
		HttpConnectionParams.setStaleCheckingEnabled(httpParameters, false);
		//@see: http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/params/HttpConnectionParams.html#setStaleCheckingEnabled%28boolean%29
        
		return new DefaultHttpClient(httpParameters);
	}
	
	public ArrayList getAll(Class clase,String url) throws ClientProtocolException, IOException, HttpException {

		DefaultHttpClient httpclient = getNewHttpClient();
		
		// Prepare a request object
		HttpGet httpmethod = new HttpGet(url);
		
		// Execute the request
		HttpResponse response;

		ArrayList stations = null;

		try {
			response = httpclient.execute(httpmethod);
			HttpEntity entity = response.getEntity();
			
			if (entity != null) {
				Gson gson = new Gson();
				String json=processContent(entity);
				Type collectionType = new TypeToken<Collection>(){}.getType();
				Collection llista = gson.fromJson(json, collectionType);
				stations.addAll(llista);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return stations;
	}
	private static String processContent(HttpEntity entity) throws IllegalStateException, IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
		StringBuilder result = new StringBuilder();
		String readLine;
		while (((readLine = br.readLine()) != null)) {
			result.append(readLine);
		}
		br.close();
		return result.toString();
	}
	private void llistaLlista(List llista){
		for (Object o:llista){
			Log.v("LLISTA",o.toString());
		}
	}
}